﻿using System;
using System.Windows.Forms;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Views.Main;
using System.ServiceModel;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost
{
    public class DistributedServicesPermisosModuleApplicationContext
        : ApplicationContext
    {

        #region Private properties

        private MainView _mainView;

        private static ServiceHost _permisosModuleServiceHost;
        
        #endregion

        #region Public properties

        public static ServiceHost PermisosModuleServiceHost
        {
            get
            {
                return _permisosModuleServiceHost;
            }
            set
            {
                _permisosModuleServiceHost = value;
            }
        }

        #endregion

        #region Constructors

        public DistributedServicesPermisosModuleApplicationContext(MainView mainView)
            : base(mainView)
        {
            _mainView = mainView;
            InitializeServices();
        }

        #endregion

        #region Private methods

        protected override void OnMainFormClosed(object sender, EventArgs e)
        {
            StopPermisosModuleServiceHost();
            base.OnMainFormClosed(sender, e);
        }

        public static void InitializeServices()
        {
        }

        public static void InitializePermisosModuleServiceHost()
        {
            _permisosModuleServiceHost = new ServiceHost(typeof(PermisosModuleService));
            _permisosModuleServiceHost.Open();
        }

        public static void StopPermisosModuleServiceHost()
        {
            if (PermisosModuleServiceHost != null)
            {
                if (PermisosModuleServiceHost.State == CommunicationState.Opened)
                {
                    PermisosModuleServiceHost.Close();
                }
                PermisosModuleServiceHost = null;
            }
        }

        #endregion

    }
}
